package mortal.game.control
{
    import Message.Db.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import flash.events.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.guild.*;
    import mortal.game.view.guild.manor.*;
    import mortal.game.view.guild.myGuild.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuildManorController extends Controller
    {
        private var _guildModule:GuildModule;
        private var _manorListWindow:GuildManorListWindow;
        private var _buyManorWindow:GuildBuyManorWindow;
        private var _auraWindow:GuildAuraWindow;
        private var _accWindow:AccelerateWindow;

        public function GuildManorController()
        {
            Dispatcher.addEventListener(EventName.GuildOpenManorListWindow, this.onGuildOpenManorListWindowHandler);
            Dispatcher.addEventListener(GuildEventName.AccWindowOpen, this.onAccWindowOpenHandler);
            Dispatcher.addEventListener(EventName.GuildGetSpaceList, this.onGuildGetSpaceListHandler);
            Dispatcher.addEventListener(EventName.GuildGetSpaceListSuccess, this.onGuildGetSpaceListSuccessHandler);
            Dispatcher.addEventListener(EventName.OpenGuildBuyManorWindow, this.onOpenGuildBuyManorWindowHandler);
            Dispatcher.addEventListener(EventName.GuildGetStructure, this.onGuildGetStructureHandler);
            Dispatcher.addEventListener(EventName.GuildGetStructureSuccess, this.onGuildGetStructureSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildUpdateStruture, this.onGuildUpdateStrutureHandler);
            Dispatcher.addEventListener(EventName.GuildUpdateStrutureSuccess, this.onGuildUpdateStrutureSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildBuyManor, this.onGuildBuyManorHandler);
            Dispatcher.addEventListener(EventName.GuildBuyManorSuccess, this.onGuildBuyManorSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildGetAward, this.onGuildGetAwardHandler);
            Dispatcher.addEventListener(EventName.GuildGetAwardSuccess, this.onGuildGetAwardSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildOpenAuraWindow, this.onGuildOpenAuraWindowHandler);
            Dispatcher.addEventListener(EventName.GuildGetAuraEvent, this.onGuildGetAuraEventHandler);
            Dispatcher.addEventListener(EventName.GuildGetAuraEventSuccess, this.onGuildGetAuraEventSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildOpenManorListWindow, this.onOpenGuildManorListWindowHandler);
            Dispatcher.addEventListener(EventName.GuildEnterManor, this.onGuildEnterManorHandler);
            Dispatcher.addEventListener(EventName.GuildQuiteManor, this.onGuildQuiteManorHandler);
            Dispatcher.addEventListener(EventName.GuildConstructAccelerate, this.onGuildConstructAccelerateHandler);
            Dispatcher.addEventListener(EventName.GuildConstructAccelerateSuccess, this.onGuildConstructAccelerateSuccessHandler);
            
        }

        public function init() : void
        {
            this._guildModule = GameController.guild.guildModule;
            
        }

        override public function get view() : IView
        {
            return this.initView();
        }

        override protected function initView() : IView
        {
            return null;
        }

        private function onAddedToStageHandler(event:Event) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildGetAuraEvent));
            
        }

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.GuildStructureUpdate, this.onGuildStructureUpdateHandler);
            
        }

        private function onOpenGuildManorListWindowHandler(event:DataEvent) : void
        {
            if (this._manorListWindow == null)
            {
                this._manorListWindow = new GuildManorListWindow();
            }
            this._manorListWindow.show();
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildGetSpaceList, {camp:0, guildName:""}));
            
        }

        private function onGuildGetSpaceListHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["camp"] as int;
            var _loc_3:* = event.data["guildName"] as String;
            GameProxy.guildProxy.getGuildSpaceList(_loc_2, _loc_3);
            
        }

        private function onGuildGetSpaceListSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Array;
            if (_loc_2.length == 0)
            {
                MsgManager.addBroadCast("没有搜索到符合条件的仙盟！");
            }
            if (this._manorListWindow && this._manorListWindow.isHide == false)
            {
                this._manorListWindow.updateGuildDataProvider(_loc_2);
            }
            
        }

        private function onGuildGetStructureHandler(event:DataEvent) : void
        {
            GameProxy.guildProxy.getGuildStructure();
            
        }

        private function onGuildGetStructureSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Array;
            cache.guild.structureArray = _loc_2;
            var _loc_3:* = cache.guild.getBuildingStructureInfo();
            if (_loc_3)
            {
                this.initStructureTimer(_loc_3.type, _loc_3.leftTime);
            }
            if (this._guildModule && this._guildModule.isHide == false && GuildModule.curTabType == GuildTabType.GuildBuilding)
            {
                this._guildModule.updateBuilding(_loc_2);
            }
            
        }

        private function onOpenGuildBuyManorWindowHandler(event:DataEvent) : void
        {
            var _loc_2:* = cache.guild.myGuildInfo;
            if (_loc_2 == null || _loc_2.position != EGuildPosition._EGuildLeader)
            {
                MsgManager.addBroadCast("您不是盟主，没有权限购买仙盟领地！");
                
            }
            if (_loc_2.level < 2)
            {
                MsgManager.addBroadCast("仙盟等级需达到2级才可购买领地！");
                
            }
            if (this._buyManorWindow == null)
            {
                this._buyManorWindow = new GuildBuyManorWindow();
            }
            this._buyManorWindow.show();
            
        }

        private function onGuildOpenManorListWindowHandler(event:DataEvent) : void
        {
            if (this._manorListWindow == null)
            {
                this._manorListWindow = new GuildManorListWindow();
            }
            this._manorListWindow.show();
            
        }

        private function onAccWindowOpenHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as EGuildStructureType;
            if (this._accWindow == null)
            {
                this._accWindow = new AccelerateWindow();
            }
            this._accWindow.type = _loc_2;
            this._accWindow.show();
            
        }

        private function onGuildUpdateStrutureHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as EGuildStructureType;
            GameProxy.guildProxy.updateGuildStruture(_loc_2);
            
        }

        private function onGuildUpdateStrutureSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["type"] as EGuildStructureType;
            var _loc_3:* = event.data["remainTime"] as Number;
            this.initStructureTimer(_loc_2, _loc_3);
            if (this._guildModule && this._guildModule.isHide == false)
            {
                this._guildModule.updateManorInfoPanel(_loc_2, _loc_3);
            }
            MsgManager.addBroadCast("建筑升级操作成功！");
            
        }

        private function initStructureTimer(param1:EGuildStructureType, param2:Number) : void
        {
            var _loc_3:* = cache.guild.structureTimer;
            if (_loc_3 == null)
            {
                _loc_3 = new StructureTimer(param2);
                _loc_3.type = param1;
            }
            else
            {
                _loc_3.leftTime = param2;
            }
            
        }

        private function onGuildBuyManorHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Boolean;
            GameProxy.guildProxy.buyGuildSpace(_loc_2);
            
        }

        private function onGuildBuyManorSuccessHandler(event:DataEvent) : void
        {
            this._buyManorWindow.hide();
            MsgManager.addBroadCast("购买领地成功！");
            
        }

        private function onGuildGetAwardHandler(event:DataEvent) : void
        {
            GameProxy.guildProxy.getAward();
            
        }

        private function onGuildGetAwardSuccessHandler(event:DataEvent) : void
        {
            var _loc_4:String = null;
            var _loc_2:* = GuildAwardConfig.instance.getConfigByLevel(cache.guild.myGuildInfo.hallLevel);
            var _loc_3:String = "";
            if (_loc_2)
            {
                _loc_4 = GameDefConfig.instance.getEPrictUnitName(_loc_2.type);
                _loc_3 = "成功领取" + _loc_2.value + _loc_4 + "，共消耗" + _loc_2.costContribute + "仙盟贡献。";
            }
            MsgManager.addBroadCast(_loc_3);
            MsgManager.addTipText(_loc_3);
            
        }

        private function onGuildOpenAuraWindowHandler(event:DataEvent) : void
        {
            if (this._auraWindow == null)
            {
                this._auraWindow = new GuildAuraWindow();
            }
            this._auraWindow.show();
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildGetAuraEvent));
            
        }

        private function onGuildGetAuraEventHandler(event:DataEvent) : void
        {
            GameProxy.guildProxy.getGuildAuraEvent();
            
        }

        private function onGuildGetAuraEventSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Array;
            if (this._auraWindow && this._auraWindow.isHide == false)
            {
                this._auraWindow.updateAuraInfo(_loc_2);
                this._auraWindow.updateTotalAura(cache.guild.myGuildInfo.aura);
            }
            
        }

        private function onGuildEnterManorHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["guildId"] as int;
            var _loc_3:* = event.data["useContribution"] as Boolean;
            GameProxy.copy.enterGuild_async(_loc_2, _loc_3);
            if (this._manorListWindow && this._manorListWindow.isHide == false)
            {
                this._manorListWindow.hide();
            }
            
        }

        private function onGuildQuiteManorHandler(event:DataEvent) : void
        {
            GameProxy.copy.leftGuild_async();
            
        }

        private function onGuildConstructAccelerateHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["type"] as EGuildStructureType;
            var _loc_3:* = event.data["mode"] as int;
            var _loc_4:* = event.data["time"] as int;
            GameProxy.guildProxy.accelerateUpdateGuildStruture(_loc_2, _loc_3, _loc_4);
            
        }

        private function onGuildConstructAccelerateSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["type"] as EGuildStructureType;
            var _loc_3:* = event.data["remainTime"] as Number;
            this.initStructureTimer(_loc_2, _loc_3);
            
        }

        private function onGuildStructureUpdateHandler(param1:SGuildStructureInfo) : void
        {
            if (this._guildModule && this._guildModule.isHide == false)
            {
                this._guildModule.updateManorInfoPanel(param1.type, 0, true);
            }
            
        }

    }
}
